package com.miyi.controller;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.miyi.core.Controller;
import com.miyi.entity.User;
import com.miyi.inter.UserMapper;
import com.miyi.utils.MyBatisUtil;


public class HelloController extends Controller {
	
	static SqlSessionFactory sqlSessionFactory = null;
	static {
        sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
    }
	
	public void index() {
		
		this.display();
	}
	
	public void test() {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		User user=null;
		try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            user = new User("lisi", new Integer(25));
            userMapper.insertUser(user);
            sqlSession.commit();// 这里一定要提交，不然数据进不去数据库中
        } finally {
            sqlSession.close();
        }
		this.renderJSON(user.getName()+user.getId());
	}
	
	public void test2() {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		User user=null;
		try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            user = userMapper.getUserByID(1);
            System.out.println("name: " + user.getName() + "|age: "
                    + user.getAge());
        } finally {
            sqlSession.close();
        }
        this.renderJSON(user.getName()+user.getId());
	}
}
